<template>
  <div class="app-container">
    <el-form>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">
          查询
        </el-button>
        <el-button type="primary" @click="handleAdd" icon="el-icon-plus">
          新增
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="pageList" fit stripe height="615px">
      <el-table-column
        label="ID"
        prop="roleId"
        width="100"
        align="left"
      ></el-table-column>
      <el-table-column
        label="角色名称"
        prop="name"
        show-overflow-tooltip
        align="left"
      ></el-table-column>
      <el-table-column label="角色状态" width="200" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="0"
            :inactive-value="1"
            active-text="启用"
            inactive-text="禁用"
            inactive-color="#ff4949"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="权限"
        prop="isRoot"
        show-overflow-tooltip
        width="200"
        align="left"
      >
      </el-table-column> -->
      <el-table-column
        label="备注"
        prop="memo"
        width="200"
        align="left"
      ></el-table-column>
      <el-table-column
        label="创建时间"
        prop="roleKey"
        width="200"
        align="left"
      ></el-table-column>
      <el-table-column label="操作" align="left" width="200">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['/v1/rbac/role/update']"
            >修改</el-button
          >
          <el-button
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['/v1/rbac/role/delete']"
            >删除</el-button
          >
          <el-button
            type="text"
            @click="handleSetPerms(scope.row)"
            v-hasPermi="['/v1/rbac/role/action/list']"
          >
            权限设置
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <el-dialog
      append-to-body
      center
      width="800px"
      :visible.sync="visible"
      :title="title"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="form.name" placeholder="填写角色名称" />
        </el-form-item>
        <el-form-item label="角色描述" prop="memo">
          <el-input
            v-model="form.memo"
            type="textarea"
            rows="4"
            placeholder="填写角色描述"
          />
        </el-form-item>
        <el-form-item label="用户状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="0">启用</el-radio>
            <el-radio :label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="btnLoading" @click="submitForm">
          确 定
        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <SetPermsCom ref="setPerms" @getList="getList"></SetPermsCom>
  </div>
</template>

<script>
import { roleList, createRole, updateRole, deleteRole } from "@/api/role";

import SetPermsCom from "./setPerms.vue";

export default {
  name: "Roleset",
  components: { SetPermsCom },
  data() {
    return {
      loading: false,
      btnLoading: false,
      visible: false,
      title: "新增",
      pageList: [],
      queryParams: {
        page: 1,
        size: 10,
      },
      total: 0,
      form: {},
      rules: {
        name: { required: true, message: "请填写角色名称", trigger: "blur" },
        status: {
          required: true,
          message: "请选择角色状态",
          trigger: "change",
        },
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    handleUpdate(row) {
      this.form = { ...row };
      this.title = "编辑";
      this.visible = true;
    },
    handleSetPerms(row) {
      this.$refs.setPerms.init(row);
    },
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.getList();
    },
    getList() {
      this.loading = true;

      roleList(this.queryParams)
        .then((res) => {
          this.pageList = res.data.items || [];
          this.total = res.data.total || 0;
        })
        .finally(() => (this.loading = false));
    },
    handleAdd() {
      this.title = "新增";
      this.$set(this.form, "status", 0);
      this.visible = true;
    },
    handleDelete(row) {
      this.$confirm("确定要删除此角色吗？", "提示", { type: "warning" }).then(
        () => {
          deleteRole({ roleId: row.roleId }).then((res) => {
            if (res.code == 0) {
              this.$message({
                type: "success",
                message: "操作成功",
              });
              this.getList();
            } else {
              this.$message({
                type: "error",
                message: res.msg,
              });
            }
          });
        }
      );
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.btnLoading = true;

          if (this.form.roleId) {
            // 编辑
            updateRole(this.form)
              .then((res) => {
                if (res.code == 0) {
                  this.$message({
                    type: "success",
                    message: "操作成功",
                  });
                  this.cancel();
                  this.getList();
                } else {
                  this.$message({
                    type: "error",
                    message: res.msg,
                  });
                }
              })
              .finally(() => (this.btnLoading = false));
          } else {
            // 新增
            createRole(this.form)
              .then((res) => {
                if (res.code == 0) {
                  this.$message({
                    type: "success",
                    message: "操作成功",
                  });
                  this.cancel();
                  this.getList();
                } else {
                  this.$message({
                    type: "error",
                    message: res.msg,
                  });
                }
              })
              .finally(() => (this.btnLoading = false));
          }
        }
      });
    },
    cancel() {
      this.form = {};
      this.resetForm("form");
      this.visible = false;
    },
    handleStatusChange(row) {
      let text = row.status == 0 ? "启用" : "禁用";
      this.$modal
        .confirm(`确认要${text}当前角色吗？`)
        .then(function () {
          return updateRole({ ...row });
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.status = row.status == 0 ? 1 : 0;
        });
    },
  },
};
</script>

<style></style>
